<template>
  <div id="AsnDetailList">
    <v-tableTitle :btnConfig="btnConfig"
                  :tableSelectData="selectData"
                  @backList="backList"
                  @jianhuoClick="jianhuoClick"
                  @del="del"
                  @poFill="addCreate"
                  @btnTableData="btnTableData"></v-tableTitle>
    <div class="detail">
      <component :data="{detailData,argument}"
                 :is="currentComponent"></component>
    </div>
  </div>

</template>

<script>
import tableTitle from "commons/TableTitle";
import navMenu from "commons/NavMenu";
import baseMixin from "@/mixins/BaseMixin";
import config from "../Config";

export default {
  name: "DetailList",
  mixins: [baseMixin, config],
  data () {
    return {
      newFlag: false,
      detailsFlag: false,
      argument: {},
      detailData: {},
      selectData: [],
      detailNav: "1",
      componentConfig: [
        {
          name: "detailTable",
          label: "",
          component: () => import('./page/detailTable.vue')
        },
        {
          name: "tableDetail",
          label: this.$t('common.detail'),
          component: () => import('./page/tableDetail.vue')
        },
      ],
    }
  },
  computed: {
    btnConfig () {
      if (this.detailNav == 1) {
        return [
          { label: this.$t('common.delete'), certigier: this.deleteDetailStatus||!this.btnPermNameList.includes('WM05020316'), disabled: true, invalid: "del" },
          { label: this.$t('wave.pickconfirm'), certigier: this.deleteDetailStatus||!this.btnPermNameList.includes('WM05020317'), invalid: "jianhuoClick" },
          {
            type: "table",
            certigier: this.orderStatus||!this.btnPermNameList.includes('WM05020318'),
            label: this.$t('wave.addorder'),
            invalid: "po",
            tableConfig: [
              {
                label: this.$t('config.shipper'),
                prop: "storerkey",
                input: "input",
              },
              {
                label: this.$t('out.externalorderkey2'),
                prop: "orderkey",
                input: "input",
              },
              {
                label: this.$t('common.status'),
                prop: "status",
                input: "select",
                options: this.getOptions("queryOrderStatusDropDown")
              },
              {
                label: this.$t('out.externorderkey'),
                prop: "externorderkey",
                input: "input",
              },
              {
                label: this.$t('wave.rtxtxno'),
                prop: "rtxtxno",
                input: "input",
              },
              {
                label: this.$t('out.rtxshopname'),
                prop: "rtxshopname",
                input: "input",
              },
              {
                label: this.$t('in.podate1'),
                prop: "orderdate",
                input: "date",
              },
              {
                label: this.$t('out.originalqty'),
                prop: "totalqty",
                input: "input",
                disabled: true
              },
              {
                label: this.$t('wave.totalorderlines'),
                prop: "totalorderlines",
                input: "input",
                disabled: true
              },
              {
                label: this.$t('config.carrierCode'),
                prop: "carriercode",
                input: "input",
              },
              {
                label: this.$t('out.rtxbuyermsg'),
                prop: "rtxbuyermsg",
                input: "input",
              },
              {
                label: this.$t('out.contact1'),
                prop: "c_contact1",
                input: "input",
              },
              {
                label: this.$t('config.phone'),
                prop: "c_phone2",
                input: "input",
              },
              {
                label: this.$t('out.c_city'),
                prop: "c_city",
                input: "input",
              },
              {
                label: this.$t('out.rtxcancelmark'),
                prop: "rtxcancelmark",
                input: "input",
              }
            ],
          },
        ]
      } else {
        return [
          { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM05020319'), invalid: "backList" },
          {
            type: "table",
            certigier: this.orderStatus||!this.btnPermNameList.includes('WM05020318'),
            label: this.$t('wave.addorder'),
            invalid: "po",
            tableConfig: [
              {
                label: this.$t('config.shipper'),
                prop: "storerkey",
                input: "input",
              },
              {
                label: this.$t('out.externalorderkey2'),
                prop: "orderkey",
                input: "input",
              },
              {
                label: this.$t('common.status'),
                prop: "status",
                input: "select",
                options: this.getOptions("queryOrderStatusDropDown")
              },
              {
                label: this.$t('out.externorderkey'),
                prop: "externorderkey",
                input: "input",
              },
              {
                label: this.$t('wave.rtxtxno'),
                prop: "rtxtxno",
                input: "input",
              },
              {
                label: this.$t('out.rtxshopname'),
                prop: "rtxshopname",
                input: "input",
              },
              {
                label: this.$t('in.podate1'),
                prop: "orderdate",
                input: "date",
              },
              {
                label: this.$t('out.originalqty'),
                prop: "totalqty",
                input: "input",
                disabled: true
              },
              {
                label: this.$t('wave.totalorderlines'),
                prop: "totalorderlines",
                input: "input",
                disabled: true
              },
              {
                label: this.$t('config.carrierCode'),
                prop: "carriercode",
                input: "input",
              },
              {
                label: this.$t('out.rtxbuyermsg'),
                prop: "rtxbuyermsg",
                input: "input",
              },
              {
                label: this.$t('out.contact1'),
                prop: "c_contact1",
                input: "input",
              },
              {
                label: this.$t('config.phone'),
                prop: "c_phone2",
                input: "input",
              },
              {
                label: this.$t('out.c_city'),
                prop: "c_city",
                input: "input",
              },
              {
                label: this.$t('out.rtxcancelmark'),
                prop: "rtxcancelmark",
                input: "input",
              }
            ],
          },
        ]
      }
    },
    currentComponent () {
      return this.componentConfig[this.detailNav - 1].component;
    }
  },
  methods: {
    jianhuoClick (data) {
      console.log(JSON.stringify(data))
    },
    //删除数据
    async del (data) {
      try {
        let res = await this.$api.post(`/deleteWavedetailOrders/${this.argument.wavekey}`, data);
        
          this.$message.success(res.returnMgr);
          this.$children[1].getTableData(1, {})
        
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.$children[1].$refs.table.delSelectData(data);
      }
    },
    //选中的数据
    setSelectData (data) {
      this.selectData = data;
    },
    //返回试图
    backList () {
      this.detailsFlag = false;
      this.detailNav = "1";
    },
    //新建
    async newCreate () {
      this.detailsFlag = true;
      this.detailNav = '2'
    },
    async btnTableData(page, date, callback, searchData) {
      if(this.argument.status != '0'){
        this.$message.error(`状态不为未开始，不能添加订单！`);
        return;
      }
      try {
        let res = await this.$api.post(`getOrdersList/${this.argument.wavekey}/${this.argument.rtxwavetype}/${page}`, searchData);
        callback(res);
      } catch (e) {
        this.$message.error(e);
      }
    },
    async addCreate (data) {
      let arr = [];
      arr.push(data);
      await this.$api.post(`/addWaveOrder/${this.argument.wavekey}`, arr).then(res => {
        
          // this.$refs.detail.getData();
          this.$children[1].getTableData(1, {});
          this.$message.success(res.returnMgr);
        
      })
    },
  },
  mounted () {
    
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.argument = data.routeData;
      this.detailData = data.detailList;
      this.detailNav = data.detailListNav;
    } else {
      this.argument = this.$store.state.routeData;
    }
    if(this.argument.status == '0'){
      this.orderStatus = true;
    }else{
      this.orderStatus = false;
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-navMenu": navMenu,
  },
  watch: {
    selectData (data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteDetailStatus = false;
        } else {
          this.deleteDetailStatus = true;
        }
      }
    }
  }
}
</script>

<style>
</style>
